<correlation target="area">
    <condition filed="area_id" value="area.id" sql="true"></condition>
</correlation>

<input name="id" field-meta java-type="string" data-type="varchar2(256)"
       validator="min(8)" validator-msg="id长度不低于8" can-update="false" primary-key="true"/>
<input name="username" field-meta  remark="用户名" java-type="string" can-update="true" not-null="true" data-type="varchar2(256)"/>
<input name="area_id" field-meta   remark="地区主键" java-type="int" can-update="true" data-type="number(32,0)"/>

<!--
    //定义触发器，这些触发器将在服务器上执行，而不是在客户端
    //触发器语言目前支持 js,groovy,以及表达式语言，orgl和spel
-->

<!--查询前触发
<script trigger="select.before" language="js">
    var user = param.get("user");
    //添加条件
    if(user!=null){
        param.where({area_id:user.get('area_id')});
    }else{
        param.where({area_id:'-1'});
    }
    function test(){
        return "我是javascript";
    }
    println(test());
    return true;
</script>
-->
<script trigger="select.before" language="groovy">
    def user = param.get("user");
    //添加条件
    if(user!=null)
        param.where([area_id:user.get('area_id')]);
    // else
    //   param.where([area_id:-1]);
    return true;
</script>

<script trigger="select.done" language="groovy">
    println "总计"+data.size();
    return true;
</script>
<script trigger="select.wrapper.instance" language="groovy">
    return [data:new java.util.LinkedHashMap()];
</script>

<script trigger="select.wrapper.done" language="groovy">
     if(instance.area.name==null)
        instance.area.name="无";
</script>

<script trigger="insert.before" language="groovy">
    println param.data;
    return true;
</script>

<script trigger="update.before" language="groovy">
    println param.data;
    param.where([area_id$NOT:1]);
    param.set("area_id",1);
    return true;
</script>

<script trigger="update.done" language="groovy">
    println param.data;
    println length+"条数据被更新";
    return true;
</script>

<script trigger="delete.before" language="groovy">
    import org.webbuilder.sql.param.query.QueryParam;

    //查询哪些数据将被删除,不执行触发器
    def q_param = new QueryParam(param);
    def data = table.createQuery().list(q_param);
     if(data.size()==0){
        return [success:false,message:"没有数据被删除"];
     }
    println data.size();
    return true;
</script>


<script trigger="delete.done" language="groovy">
    println length+"条数据被删除";
    return true;
</script>

